﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace com.intel.wizard.WizardUtilities
{
    public class Node
    {

        private Node nodeLeft;
        private Node nodeRight;
        //0 = +(OR), 1 = *(AND), 2 = -(XOR)
        private int operation;
        private WebControl control;
        private string expresion;

        public Node(int oper, WebControl control, string expr)
        {
            this.operation = oper;
            this.control = control;
            this.expresion = expr;
        }

        public void setNodeLeft(Node node)
        {
            this.nodeLeft = node;
        }

        public void setNodeRight(Node node)
        {
            this.nodeRight = node;
        }

        public bool calculateExpresion()
        {
            bool result = true;

            if (this.nodeLeft != null)
            {
                result = this.nodeLeft.calculateExpresion();
            }

            if (this.nodeRight != null)
            {
                switch (this.operation)
                {
                    case 0:
                        result = result | this.nodeRight.calculateExpresion();
                        break;
                    case 1:
                        result = result & this.nodeRight.calculateExpresion();
                        break;
                    case 2:
                        result = result ^ this.nodeRight.calculateExpresion();
                        break;
                }
            }
            else
            {
                Regex regex = new Regex(this.expresion);
                string value = this.stringValueFromControl();
                result = regex.IsMatch(value);
            }

            return result;
        }

        private string stringValueFromControl()
        {
            return ((TextBox)control).Text;
        }

    }
}
